#ifndef DOWNLOAD_H
#define DOWNLOAD_H
#include <QtCore>
#include <QtNetwork>
#include <QTime>
#include<QSqlDatabase>
#include<QSqlRecord>
#include "db.h"
#include <QMessageBox>
#include<QList>
#include<QStringList>
//#include<stdio.h>
#include<QThread>
#include<QTime>


//多线程下载的线程数
const int PointCount = 5;

//用于下载文件（或文件的一部分）
class Download : public QObject
{
        Q_OBJECT
private:
        QNetworkAccessManager m_Qnam;
        QNetworkReply *m_Reply;
        QFile *m_File;
        QUrl url;
        const int m_Index;
        qint64 m_HaveDoneBytes;
        qint64 m_StartPoint;
        qint64 m_MiddlePoint;
        qint64 m_EndPoint;
        QSqlDatabase db;
        bool isc;

public:
        Download(int index, QSqlDatabase db,QObject *parent = 0);

        void StartDownload(const QUrl &url, QFile *file, qint64 startPoint,qint64 middlePoint, qint64 endPoint);
        void StartDownload(const QUrl &url, QFile *file,
                qint64 startPoint=0, qint64 endPoint=-1);
        void StopDownload(void);
signals:
        void DownloadFinished();
        void SendWriteData(qint64 data);
        public slots:
                void FinishedSlot();
        protected slots:
                void HttpReadyRead();
};

//用于管理文件的下载
class DownloadControl : public QObject
{
        Q_OBJECT
private:
        int m_DownloadCount;
        int m_FinishedNum;
        int m_FileSize;
        qint64 m_DataSize;
        QUrl m_Url;
        QString strUrl;
        QFile *m_File;
        QTime *m_Time;
        QString m_Path;
        int rate;
        QSqlDatabase db;
        QList<Download*> downlist;
public:
        DownloadControl(QObject *parent = 0);
        void SetSavePath(const QString& path);
        void StartFileDownload(const QString &url, int count);
        qint64 GetFileSize(QUrl url);
        void GetFileSizeAndFileName(QUrl url,QUrl& newurl,int &size,QString &filename);
        void setDataBase(const QSqlDatabase& db);
        void Init(const QString& url,int count);
        void run();
    public slots:
        void StopDownload(const QString& filename);
        void ContinueDownload(const QString& filename);

signals:
        void ChangeTodayTable(void);
        void FileDownloadFinished();
        void DownloadInfo(const QString& file,const int& Rate,const int& FileSize,const QString& path);
        private slots:
                void SubPartFinished();
                void CounterRate(qint64 data);

};
#endif // DOWNLOAD_H
